package org.fas.utils
{
	public class FuString
	{
		public function FuString()
		{
			throw new Error("FuString class is static class only,it can't be constructed!");  
		}
		/**
		 * only show the string last chars 
		 * @param _str
		 * @param _charSum
		 * @return 
		 * 
		 */		
		public static function showLastChars(_str:String,_showLength:int=4,_coverLength:int=-1):String{
			var _length:int = _str.length;
			_coverLength = _coverLength!=-1?_coverLength:_length-_showLength;
			_str = _str.substr(-_showLength,_showLength);
			for(var i:int=0;i<_coverLength;i++){
				_str = ('*'+_str);
			}
			return _str;
		}
		public static function replaceAll(char:String, replace:String, replaceWith:String):String{              
              return char.split(replace).join(replaceWith);  
         }
		public static function replaceAll2(_str0:String,_pattern:String,_repl:String):String{
			if(_str0.indexOf(_pattern)==-1){
				return _str0;
			}
			return replaceAll2(_str0.replace(_pattern,_repl),_pattern,_repl);
		}
		public static function getBetweenTags(_oldString:String,_frontTag:String,_backTag:String):String{
			var _arr:Array = _oldString.split(_frontTag);
			_arr = String(_arr[1]).split(_backTag);
			return String(_arr[0]);
		}
		/*public static function insertBetweenTags(_oldString:String,_newContent:String,_frontTag:String,_backTag:String):String{
			
		}*/
		public static function updateBetweenTags(_oldString:String,_newContent:String,_frontTag:String,_backTag:String):String{
			var _arr1:Array = _oldString.split(_frontTag);
			var _arr2:Array = String(_arr1[1]).split(_backTag);
			return _arr1[0]+_frontTag+_newContent+_backTag+_arr2[1];
		}
         public static function equalsIgnoreCase(char1:String,char2:String):Boolean{  
             return char1.toLowerCase() == char2.toLowerCase();  
         }  
         public static function equals(char1:String,char2:String):Boolean{  
             return char1 == char2;  
         }  
         public static function isEmail(char:String):Boolean{  
             if(char == null){  
                 return false;  
             }  
             char = trim(char);  
             var pattern:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;   
             var result:Object = pattern.exec(char);  
             if(result == null) {  
                 return false;  
             }  
            return true;  
         }  
          public static function isNumber(char:String):Boolean{  
             if(char == null){  
                   return false;  
               }  
              return !isNaN(Number(char))  
          }  
          public static function isDouble(char:String):Boolean{  
              char = trim(char);  
              var pattern:RegExp = /^[-\+]?\d+(\.\d+)?$/;   
             var result:Object = pattern.exec(char);  
              if(result == null) {  
                 return false;  
               }  
               return true;  
          }  
          //Integer;  
          public static function isInteger(char:String):Boolean{  
             if(char == null){  
                  return false;  
             }  
              char = trim(char);  
              var pattern:RegExp = /^[-\+]?\d+$/;   
               var result:Object = pattern.exec(char);  
             if(result == null) {  
                  return false;  
              }  
              return true;  
          }  
          //English;  
         public static function isEnglish(char:String):Boolean{  
              if(char == null){  
                   return false;  
             }  
             char = trim(char);  
             var pattern:RegExp = /^[A-Za-z]+$/;   
             var result:Object = pattern.exec(char);  
              if(result == null) {  
                 return false;  
             }  
              return true;  
           }  
          public static function isChinese(char:String):Boolean{  
             if(char == null){  
                  return false;  
              }  
              char = trim(char);  
             var pattern:RegExp = /^[\u0391-\uFFE5]+$/;   
               var result:Object = pattern.exec(char);  
               if(result == null) {  
                  return false;  
              }  
               return true;  
           }  
           public static function isDoubleChar(char:String):Boolean{  
              if(char == null){  
                  return false;  
              }  
              char = trim(char);  
              var pattern:RegExp = /^[^\x00-\xff]+$/;   
              var result:Object = pattern.exec(char);  
               if(result == null) {  
                   return false;  
               }  
               return true;  
           }  
         public static function hasChineseChar(char:String):Boolean{  
              if(char == null){  
                  return false;  
             }  
            char = trim(char);  
             var pattern:RegExp = /[^\x00-\xff]/;   
             var result:Object = pattern.exec(char);  
             if(result == null) {  
                 return false;  
             }  
              return true;  
          }  
          public static function hasAccountChar(char:String,len:uint=15):Boolean{  
             if(char == null){  
                  return false;  
            }  
              if(len < 10){  
                  len = 15;  
              }  
            char = trim(char);  
             var pattern:RegExp = new RegExp("^[a-zA-Z0-9][a-zA-Z0-9_-]{0,"+len+"}$", "");   
             var result:Object = pattern.exec(char);  
              if(result == null) {  
                  return false;  
              }  
             return true;  
          }  
          public static function isURL(char:String):Boolean{  
             if(char == null){  
                 return false;  
             }  
             char = trim(char).toLowerCase();  
             var pattern:RegExp = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/;   
             var result:Object = pattern.exec(char);  
              if(result == null) {  
                  return false;  
             }  
              return true;  
         }  
         public static function isWhitespace(char:String):Boolean{  
             switch (char){  
                 case " ":  
                case "\t":  
                  case "\r":  
                 case "\n":  
                  case "\f":  
                      return true;      
                default:  
                     return false;  
              }  
          }  
          public static function trim(char:String):String{  
             if(char == null){  
                 return null;  
             }  
              return rtrim(ltrim(char));  
          }  
          public static function ltrim(char:String):String{  
             if(char == null){  
                  return null;  
              }  
              var pattern:RegExp = /^\s*/;   
             return char.replace(pattern,"");  
          }  
         public static function rtrim(char:String):String{  
              if(char == null){  
                  return null;  
              }  
              var pattern:RegExp = /\s*$/;   
              return char.replace(pattern,"");  
          }  
          //is front suffix words;  
         public static function beginsWith(char:String, prefix:String):Boolean{              
              return (prefix == char.substring(0, prefix.length));  
          }  
            
          //is back suffix words;  
          public static function endsWith(char:String, suffix:String):Boolean{  
             return (suffix == char.substring(char.length - suffix.length));  
          }  
           
         	public static function removeAll(char:String,remove:String):String{  
              return replaceAll(char,remove,"");  
          }  
           
          //utf16 - utf8 ;  
          public static function utf16to8(char:String):String{  
              var out:Array = new Array();  
              var len:uint = char.length;  
              for(var i:uint=0;i<len;i++){  
                  var c:int = char.charCodeAt(i);  
                if(c >= 0x0001 && c <= 0x007F){  
                    out[i] = char.charAt(i);  
                  } else if (c > 0x07FF) {  
                      out[i] = String.fromCharCode(0xE0 | ((c >> 12) & 0x0F),  
                                                   0x80 | ((c >>  6) & 0x3F),  
                                                   0x80 | ((c >>  0) & 0x3F));  
                  } else {  
                      out[i] = String.fromCharCode(0xC0 | ((c >>  6) & 0x1F),  
                                                   0x80 | ((c >>  0) & 0x3F));  
                  }  
              }  
              return out.join('');  
          }  
        public static function utf8to16(char:String):String{  
             var out:Array = new Array();  
            var len:uint = char.length;  
              var i:uint = 0;  
              var char2:int
              while(i<len){  
                  var c:int = char.charCodeAt(i++);  
                  switch(c >> 4){  
                      case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:  
                          // 0xxxxxxx  
                          out[out.length] = char.charAt(i-1);  
                         break;  
                     case 12: case 13:  
                          // 110x xxxx   10xx xxxx  
                          char2 = char.charCodeAt(i++);  
                          out[out.length] = String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));  
                          break;  
                      case 14:  
                          // 1110 xxxx  10xx xxxx  10xx xxxx  
                         char2 = char.charCodeAt(i++);  
                         var char3:int = char.charCodeAt(i++);  
                         out[out.length] = String.fromCharCode(((c & 0x0F) << 12) |  
                              ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0));  
                        break;  
               }  
             }  
            return out.join('');  
         }  
	}
}